
import 'package:architecture/shared_preference.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/api/api.dart';
import 'package:study_demo/api/bean.dart';
import 'package:study_demo/base/BaseController.dart';
import 'package:study_demo/constant/keys.dart';

class BoardActivateStateController extends BaseController {
  final status = 10.obs;
  final error = ''.obs;
  final userName = ''.obs;
  final country = 1.obs;
  final boardHistoryData = <String>[].obs;

  String deviceId = '';
  int boardId = 0;

  var info = BoardInfo();

  TextEditingController _textEditingController = TextEditingController();

  TextEditingController get textEditingController => _textEditingController;

  @override
  onInit() {
    super.onInit();
    String data = readString(BOARD_HISTORY_DATA) ?? '';
    boardHistoryData.value = data.split(',');
    boardHistoryData.removeWhere((element) => element.isEmpty);
  }

  activateStatus() {
    String code = _textEditingController.text;
    if (code.isEmpty) {
      // toast('棋盘编号不能为空');
      error.value = '棋盘编号不能为空';
      status.value = 10;
      return;
    }
    if (code.length != 9 || !code.isNum) {
      // toast('棋盘编号格式不正确');
      error.value = '棋盘编号格式不正确';
      status.value = 10;
      return;
    }
    saveData(code);
    api.get(RequestModel(board_info, boardInfo(code)), success: (json) {
      status.value = 0;
      info = BoardInfo.fromJson(json);
      boardId = info.boardid!;
      country.value = info.country!;
      print('boardId:$boardId');
      if (boardId > 0) {
        status.value = 1;
        error.value = '';
      } else {
        status.value = 0;
      }
    }, error: (msg) {
      toast(msg);
      status.value = 0;
    }, loading: false);
  }

  _boardStatus(String deviceId) {
    api.get(RequestModel(activate_status, activateStatusInfo(deviceId)),
        success: (json) {});
  }

  _boardDetailInfo() {
    api.get(
        RequestModel(board_detail_info, boardDetailInfo(deviceId),
            sNum: '1', userId: 0), success: (json) {
      userName.value = json[''];
    });
  }

  changeCountry(int changedCountry) {
    api.get(RequestModel(login_code, loginInfo('罗彪', '123456'), userId: 0),
        success: (json) {
      String dypassword = json['dypassword'];
      int userId = json['userid'];
      info.country = changedCountry;
      api.post(
          RequestModel(change_board_info, changeBoardInfo(info),
              sNum: dypassword, userId: userId), success: (json) {
        country.value = changedCountry;
        toast('修改成功');
      });
    });
  }

  saveData(String data) {
    String historyData = readString(BOARD_HISTORY_DATA) ?? '';

    if (!historyData.contains(data)) {
      saveString(BOARD_HISTORY_DATA, historyData + ',' + data);
      historyData = historyData + ',' + data;
    }
    boardHistoryData.value = historyData.split(',');
    boardHistoryData.removeWhere((element) => element.isEmpty);

    print('length:${boardHistoryData}');
  }

  setData(String data) {
    _textEditingController.text = data;
  }

  deleteData(String data) {
    boardHistoryData.removeWhere((element) => element == data);
    String boardHistoryDataStr = '';
    boardHistoryData.forEach((element) {
      boardHistoryDataStr += element + ',';
    });
    saveString(BOARD_HISTORY_DATA, boardHistoryDataStr);
  }
}
